'
' Copyright (c) 2004-2005
' by Peter Donker ( info@bring2mind.net )
'
' Permission is hereby granted to use and/or modify this software for a single server. It is 
' NOT permitted to publish, distribute, sublicense, and/or sell copies of this software in its
' original or modified form. You are not allowed to remove this copyright notice.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
' BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
' DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF THE SOFTWARE.
'
Imports System.Drawing.Imaging

Namespace Bring2mind.DNN.Modules.Guestbook
 Module Globals
  Public Const PermittedFileExtensions As String = ".tif;.tiff;.jpg;.jpeg;.gif;"

  Public Sub ResizeImage(ByVal sFilename As String, ByVal iSize As Integer, Optional ByVal DestinationPath As String = "")
   Dim thisImage As Bitmap
   Dim imgFormat As ImageFormat
   Dim sFile$, sExt$, sDir$
   Dim imgHeight As Integer, imgWidth As Integer, imgRatio As Single

   Dim fi As System.IO.FileInfo
   Dim xProp As Integer

   Try

    fi = New System.IO.FileInfo(sFilename)
    sDir$ = fi.DirectoryName
    sExt$ = fi.Extension
    sFile$ = Left$(fi.Name, Len(fi.Name) - Len(sExt$))

    thisImage = New Bitmap(sFilename)
    imgFormat = thisImage.RawFormat
    imgWidth = thisImage.Width
    imgHeight = thisImage.Height
    imgRatio = Convert.ToSingle(imgHeight / imgWidth)

    Dim backBuffer As Bitmap
    Dim backBufferGraphics As Graphics
    Dim xNewHeight As Integer, xNewWidth As Integer
    Dim xScale As Single

    If imgRatio > 1 Then
     xScale = Convert.ToSingle(iSize / imgHeight)
    Else
     xScale = Convert.ToSingle(iSize / imgWidth)
    End If
    If xScale > 1 Then
     fi = Nothing
     thisImage.Dispose()
     Exit Sub
    End If
    xNewHeight = Convert.ToInt32(imgHeight * xScale)
    xNewWidth = Convert.ToInt32(imgWidth * xScale)

    backBuffer = New Bitmap(xNewWidth, xNewHeight, Drawing.Imaging.PixelFormat.Format32bppRgb)
    backBufferGraphics = Graphics.FromImage(backBuffer)
    backBufferGraphics.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
    backBufferGraphics.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
    backBufferGraphics.DrawImage(thisImage, 0, 0, xNewWidth, xNewHeight)

    thisImage.Dispose()
    fi = Nothing

    If DestinationPath <> "" Then
     sFilename = IO.Path.Combine(DestinationPath, IO.Path.GetFileName(sFilename))
    End If

    backBuffer.Save(sFilename, imgFormat)

   Catch ex As Exception

    thisImage.Dispose()
    fi = Nothing
    Throw New Exception(ex.Message)

   End Try

  End Sub

 End Module
End Namespace